package spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.bean.BestStudent;
import spring.bean.Mali;
import spring.bean.Student;
import spring.listener.event.MyEvent;
import spring.task.MainConfiguration;

import java.io.File;
import java.io.IOException;

public class mainDemo {
    public static void main(String[] args) throws IOException {
        getAopTest();
//        System.out.println(new File("zombie/zombie01.png").getCanonicalPath());//打印文件路径
    }

    public static void GetBean(){
        ApplicationContext context = new ClassPathXmlApplicationContext("test.xml");
        Student student = (Student) context.getBean("student");   //使用getBean方法来获取对应的对象（Bean）
        student.hello();
    }

    public static void GetClass(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test.xml");
        //getBean有多种形式，其中第一种就是根据类型获取对应的Bean
        //容器中只要注册了对应类的Bean或是对应类型子类的Bean，都可以获取到
        Mali mali = context.getBean(Mali.class);
        mali.hello();
        context.close();
    }

    public static void GetBestStudent() throws InterruptedException{
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
        BestStudent student = context.getBean(BestStudent.class);
        context.publishEvent(new MyEvent(context));
        System.out.println(student.getClass());
        student.asyncTest();
        student.syncTest();
        context.close();
    }

    public static void getAopTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");
        spring.aop.Student bean = context.getBean(spring.aop.Student.class);
        bean.study("Godot");
        bean.studyJava("Java");
    }
}
